package demo.hms.Live.service.servicelmpl;

import demo.hms.Live.dao.LiveDao;
import demo.hms.Live.service.LiveService;
import demo.hms.liverecords.service.LiveRecordsService;
import demo.hms.mybatis.entity.Live;
import demo.hms.mybatis.entity.LiveRecords;
import demo.hms.mybatis.entity.Stay;
import demo.hms.stay.dao.StayDao;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class LiveServicelmpl implements LiveService {

    @Resource
    private StayDao dao;

    @Resource
    private LiveDao liveDao;

    @Resource
    private LiveRecordsService liveRecordsService;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Result selectStay(Stay stay) {
        return ResultSubmit.success(dao.selectStay(stay));
    }
    @Override
    public Result insertStay(Live live) {
        live.setDeleteIdentity("未删除");live.setLiveTime(format.format(new Date()));
        live.setLiveId(Math.abs((int)System.currentTimeMillis()));
        live.setAbrogateIdentity("未作废");live.setCollectionIdentity("未收款");
        live.setDataUpdateTime(format.format(new Date()));
        LiveRecords liveRecords = new LiveRecords(); liveRecords.setLiveId(live.getLiveId());
        liveRecords.setLiveRecordsId(Math.abs((int)System.currentTimeMillis()));
        liveRecords.setCustomerId(live.getCustomerId());liveRecords.setRoomId(live.getRoomId());
        liveRecords.setDataUpdateTime(format.format(new Date())); liveRecords.setDeleteIdentity("未删除");
        if (liveDao.insertLive(live) == 1 && liveRecordsService.insert(liveRecords) == 1) {
            return ResultSubmit.success(live.getLiveId());}else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return ResultSubmit.fail();
        }
    }
    @Override
    public Result cancelStay(Live live) {
        live = liveDao.queryAll(live).get(0);
        int i = liveDao.updateLive(live);
        live.setCancelTime(format.format(new Date()));
        int j = liveDao.insertLive(live);
        if(i == 1 && j == 1){
            return ResultSubmit.success();
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }

    @Override
    public Result abrogateStay(Live live) {
        live = liveDao.queryAll(live).get(0);
        int i = liveDao.updateLive(live);
        live.setAbrogateIdentity("已作废");
        int j = liveDao.insertLive(live);
        if(i == 1 && j == 1){
            return ResultSubmit.success();
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }
    @Override
    public Result settlementLive(Live live) {
        live = liveDao.queryAll(live).get(0);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date liveTime = df.parse(live.getLiveTime());//入住时间
            Date cancelTime=df.parse(live.getCancelTime());//退房时间
            Long num= cancelTime.getTime()-liveTime.getTime();//时间戳相差的毫秒数
            BigDecimal day = BigDecimal.valueOf(num/24/60/60/1000).add(BigDecimal.ONE);//除以一天的毫秒数计算天数
            live.setLiveAmount(day.multiply(live.getRoomPrice()));//并计算金额
        } catch (ParseException e) {e.printStackTrace();}
        int i = liveDao.updateLive(live);
        int j = liveDao.insertLive(live);
        if(i == 1 && j == 1){
            return ResultSubmit.success();
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail(); } }

}
